package com.leetcode.problems006;


import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {


    public String convert(String s, int numRows) {
        if(s == null || s.length() <= numRows || numRows <= 1){
            return s;
        }
        List<StringBuilder> list = new ArrayList<>(numRows);
        for (int i = 0; i < numRows; i++) {
            list.add(new StringBuilder());
        }
        StringBuilder sb = new StringBuilder();
        int curRow = 0;
        boolean plus = false;
        for (int curIndex = 0; curIndex < s.length(); curIndex++) {
            list.get(curRow).append(s.charAt(curIndex));
            if (curRow == 0 || curRow == numRows - 1) {
                plus = !plus;
            }
            curRow += plus ? 1 : -1;
        }
        for (StringBuilder stringBuilder : list) {
            sb.append(stringBuilder);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution().convert("AB",1));
    }
}
